Ext.define('WGenius.controller.Contacters', {
	extend: 'Ext.app.Controller',

	config: {
		refs: {
			ContacterContainer: 'ContacterContainer',
			Contacters: 'ContacterContainer Contacters',
			Contacter: 'ContacterContainer Contacter',
			ContacterInfo: 'ContacterContainer ContacterInfo',
			sessions: 'ContacterContainer Contacter list'
		},
		control: {
			Contacters: {
				itemtap: 'onContacterTap',
				activate: 'onContactersActivate'
			},
			sessions: {
				itemtap: 'onSessionTap'
			}
		}
	},

	onContacterTap: function(list, idx, el, record) {
		var sessionStore = Ext.getStore('ContacterSessions'),
			sessionIds = record.get('sessionIds');

		sessionStore.clearFilter(true);
		sessionStore.filterBy(function(session) {
			return Ext.Array.contains(sessionIds, session.get('id'));
		});

		if (!this.Contacter) {
			this.Contacter = Ext.widget('Contacter');
		}

		this.Contacter.config.title = record.getFullName();
		this.getContacterContainer().push(this.Contacter);
		this.getContacterInfo().setRecord(record);
	},

	onSessionTap: function(list, idx, el, record) {

		if (!this.sessionInfo) {
			this.sessionInfo = Ext.widget('sessionInfo');
		}

		this.sessionInfo.config.title = record.get('title');
		this.sessionInfo.setRecord(record);
		this.getContacterContainer().push(this.sessionInfo);
	},

	onContactersActivate: function() {
		if (this.Contacter) {
			this.Contacter.down('list').deselectAll();
		}
	}

});
